package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import play.db.DB;

public class DAOConnection {
	private static Connection connection = null;
 	
	public static Connection getConnection() {

		connection = DB.getConnection();
		return connection;
    }

	public static void closeConnection() {
		try {
			if (connection != null) connection.close();
		} catch (SQLException e) {
			System.out.println("Erro fechando conexao: " + e);
		}
	}
	
	public static String date2StringSQL(Date data) {
		//return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(data);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(data);
	}
	
	public static String date2StringPTBR(Date data) {
		//return (new SimpleDateFormat("dd/MM/yyyy HH:mm:ss")).format(data);
		return (new SimpleDateFormat("dd/MM/yyyy")).format(data);
	}
	
	
	public static Date string2DateSQL(String data) {
		try {
			//return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(data);			
			return (new SimpleDateFormat("yyyy-MM-dd")).parse(data);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Date string2DatePTBR(String data) {
		try {
			//return (new SimpleDateFormat("dd/MM/yyyy HH:mm:ss")).parse(data);
			return (new SimpleDateFormat("dd/MM/yyyy")).parse(data);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Date string2DateHTML(String data) {
		try {
			return (new SimpleDateFormat("dd/MM/yyyy")).parse(data);			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
